package com.czy.basecommon.utils.view;

import android.animation.Animator;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.czy.basecommon.R;
import com.czy.basecommon.utils.tools.AnimUtil;
import com.czy.basecommon.utils.tools.DensityTool;

public class PopUpManager {
    private static final long DURATION = 500;
    private static final float START_ALPHA = 0.7f;
    private static final float END_ALPHA = 1f;
    private float bgAlpha = 1f;
    private boolean bright = false;
    private AnimUtil animUtil;
    public MyBasicDataCallBack mDataCallBack;

    //数据回调接口
    public interface MyBasicDataCallBack<T> {
        void onViewClickCallBack( T t);
    }


    private static class SingletonClassInstance{
         private static final PopUpManager instance=new PopUpManager();

      }
              private PopUpManager(){
                  animUtil=new AnimUtil();
              }

              public static PopUpManager getInstance() {
                  return SingletonClassInstance.instance;
              }
    public void showPop(PopupWindow mPopupWindow, Context context,int resid, View view,MyBasicDataCallBack<String> mDataCallBack) {

        // 设置布局文件
        mPopupWindow.setContentView(LayoutInflater.from(context).inflate(resid, null));
        View contentView = mPopupWindow.getContentView();
        View l1 = contentView.findViewById(R.id.l1);
        View l2 = contentView.findViewById(R.id.l2);
        l1.setOnClickListener(v->{
            mDataCallBack.onViewClickCallBack("1");
            mPopupWindow.dismiss();
        });
        l2.setOnClickListener(v->{
            mDataCallBack.onViewClickCallBack("2");
            mPopupWindow.dismiss();
        });
        // 为了避免部分机型不显示，我们需要重新设置一下宽高
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置pop透明效果
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
        // 设置pop出入动画
        mPopupWindow.setAnimationStyle(R.style.pop_add);
        // 设置pop获取焦点，如果为false点击返回按钮会退出当前Activity，如果pop中有Editor的话，focusable必须要为true
        mPopupWindow.setFocusable(true);
        // 设置pop可点击，为false点击事件无效，默认为true
        mPopupWindow.setTouchable(true);
        // 设置点击pop外侧消失，默认为false；在focusable为true时点击外侧始终消失
        mPopupWindow.setOutsideTouchable(true);
        // 相对于 + 号正下面，同时可以设置偏移量
        int i = DensityTool.dip2px(context, 70);
        int d = DensityTool.dip2px(context, -10);
        mPopupWindow.showAsDropDown( view, -i, d);
        // 设置pop关闭监听，用于改变背景透明度
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                toggleBright();
            }
        });
        toggleBright();
    }

    private void toggleBright() {
        // 三个参数分别为：起始值 结束值 时长，那么整个动画回调过来的值就是从0.5f--1f的
        animUtil.setValueAnimator(START_ALPHA, END_ALPHA, DURATION);
        animUtil.addUpdateListener(new AnimUtil.UpdateListener() {
            @Override
            public void progress(float progress) {
                bgAlpha = bright ? progress : (START_ALPHA + END_ALPHA - progress);
            }
        });
        animUtil.addEndListner(new AnimUtil.EndListener() {
            @Override
            public void endUpdate(Animator animator) {
                // 在一次动画结束的时候，翻转状态
                bright = !bright;
            }
        });
        animUtil.startAnimator();
    }

}
